package com.wsc.oauth2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class OauthResourceConfig extends ResourceServerConfigurerAdapter {

    @Value("${security.oauth2.client.client-id}")
    String clientId;

    @Value("${security.oauth2.client.client-secret}")
    String secret;

    @Value("${security.oauth2.authorization.check-token-access}")
    String checkTokenEndpointUrl;

    /**
     * @Description 令牌服务,和oauth2中配置的资源信息一致
     * @Date 2019/7/15 18:07
     * @Version 1.0
     * */

//    @Bean
//    @Primary
//    public RemoteTokenServices tokenService(){
//        RemoteTokenServices tokenServices = new RemoteTokenServices();
//        tokenServices.setClientId(clientId);
//        tokenServices.setClientSecret(secret);
//        tokenServices.setCheckTokenEndpointUrl(checkTokenEndpointUrl);
//        return tokenServices;
//    }

//    @Override
//    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
//        resources.tokenServices(tokenService());
//        resources.resourceId("backservice");
//        super.configure(resources);
//    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest()
                .authenticated();
        http.csrf().disable();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
//      resources.tokenServices(initTokenService());
        resources.resourceId("backService");
        super.configure(resources);
    }
}
